using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PlacementManager;

public partial class Home_Venues : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            this.txtSearchName.Focus();
            if (Request.QueryString["id"] != null)
            {
                this.gvVenues.DataSourceID = "odsSingleVenue";
            }
            else
            {
                this.gvVenues.DataSourceID = "odsVenues_GV";
            }
        }  
    }






    #region Helper Methods
    protected string GetFormattedAddress(string addressId)
    {
        int iAddressId = Int32.Parse(addressId);

        if (iAddressId > 0)
        {
            PlacementManager.AddressComponent oAddressC = new AddressComponent();
            return oAddressC.GetFormattedAddress(iAddressId);
        }
        else
        {
            return string.Empty;
        }
    }
    protected string GetIsPrimaryAddress(string addressId)
    {
        string toReturn = string.Empty;

        PlacementManager.LinkVenueAddressComponent oLvaC = new LinkVenueAddressComponent();
        List<PlacementManager.LinkVenueAddress> oLinkList = oLvaC.SelectAllWAddressId(Int32.Parse(addressId), "AddressId");
        if (oLinkList.Count > 0)
        {
            if (oLinkList[0].IsPrimary)
            {
                toReturn = "Primary Address:<br />";
            }
        }
        return toReturn;
    }
    protected bool ShowMakePrimaryAddressButton(string addressId)
    {
        bool toReturn = false;

        PlacementManager.LinkVenueAddressComponent oLvaC = new LinkVenueAddressComponent();
        List<PlacementManager.LinkVenueAddress> oLinkList = oLvaC.SelectAllWAddressId(Int32.Parse(addressId), "AddressId");
        if (oLinkList.Count > 0)
        {
            if (oLinkList[0].IsPrimary)
            {
                toReturn = false;
            }
            else
            {
                toReturn = true;
            }
        }
        return toReturn;
    }
    protected string GetTruncatedVenueName(string venueName)
    {
        string s = string.Empty;
        if (venueName.Length > 45)
        {
            s = venueName.Substring(0, 45) + "...";
        }
        else
        {
            s = venueName;
        }
        return s;
    }
    protected string GetParentVenue(string parentVenueId)
    {
        string sToReturn = string.Empty;
        int iParentVenueId = Int32.Parse(parentVenueId);
        if (iParentVenueId > 0)
        {
            PlacementManager.VenueComponent oVenueC = new VenueComponent();
            PlacementManager.Venue oVenue = oVenueC.SelectOne(iParentVenueId);
            sToReturn = "Parent venue: " + oVenue.VenueName;
        }
        else
        {
            sToReturn = "No parent";
        }

        return sToReturn;
    }
    protected string GetContactNumber(string sContactNumberId)
    {
        PlacementManager.ContactNumberComponent o = new PlacementManager.ContactNumberComponent();
        return o.GetContactNumberWContactNumberId(Int32.Parse(sContactNumberId));
    }
    protected string GetContactNumberType(string sContactNumberId)
    {
        PlacementManager.ContactNumberComponent oCncC = new PlacementManager.ContactNumberComponent();
        PlacementManager.ContactNumber oContactNumber = oCncC.SelectOne(Int32.Parse(sContactNumberId));

        PlacementManager.ContactNumberTypeComponent oCntC = new PlacementManager.ContactNumberTypeComponent();
        return oCntC.SelectOne(oContactNumber.ContactNumberTypeId).ContactNumberTypeName;
    }
    protected string GetPrimaryAddressForContact(string contactId)
    {
        string sToReturn = string.Empty;
        PlacementManager.AddressComponent oAddressC = new AddressComponent();
        PlacementManager.Address oAddress = oAddressC.SelectPrimaryAddressForContact(Int32.Parse(contactId));
        if (oAddress.AddressId > 0)
        {
            sToReturn = oAddressC.GetFormattedAddress(oAddress.AddressId);
        }
        return sToReturn;
    }
    protected bool GetIsPrimaryContact(int contactId)
    {
        bool toReturn = false;

        PlacementManager.LinkVenueContactComponent oLvaC = new LinkVenueContactComponent();
        List<PlacementManager.LinkVenueContact> oLinkList = oLvaC.SelectAllWContactId(contactId, "ContactId");
        if (oLinkList.Count > 0)
        {
            if (oLinkList[0].IsPrimary)
            {
                toReturn = true;
            }
        }
        return toReturn;
    }
    protected bool ShowMakePrimaryContactButton(string contactId)
    {
        bool toReturn = true;

        PlacementManager.LinkVenueContactComponent oLvaC = new LinkVenueContactComponent();
        List<PlacementManager.LinkVenueContact> oLinkList = oLvaC.SelectAllWContactId(Int32.Parse(contactId), "ContactId");
        if (oLinkList.Count > 0)
        {
            if (oLinkList[0].IsPrimary)
            {
                toReturn = false;
            }
        }
        return toReturn;
    }
    protected string GetUnitCode(string unitId)
    {
        PlacementManager.UnitComponent o = new PlacementManager.UnitComponent();
        return o.SelectOne(Int32.Parse(unitId)).UnitCode;
    }
    protected string GetCampusCode(string campusId)
    {
        PlacementManager.CampusComponent o = new PlacementManager.CampusComponent();
        return o.SelectOne(Int32.Parse(campusId)).CampusCode;
    }
    protected string GetTelephoneFaxEmailNumber(string TelephoneNumberFaxEmail)
    {
        if (TelephoneNumberFaxEmail.Length == 0)
        {
            return "Not known";
        }
        else
        {
            return TelephoneNumberFaxEmail;
        }
    }

    #endregion




    /// <summary>
    /// Inserts a new venue record with an address. The address is then associated with the new venue.
    /// This needs to be in a transaction. See notes in the function.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddNewVenue_OnClick(object sender, EventArgs e)
    {



    }
    protected void fvVenueDetails_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        PlacementManager.VenueComponent oVenueC = new VenueComponent();
        PlacementManager.Venue oVenue = oVenueC.SelectOne(Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString()));


        e.NewValues.Add("VenueId", oVenue.VenueId);
        e.NewValues.Add("IsActive", true);
        e.NewValues.Add("CreatedBy", oVenue.CreatedBy);
        e.NewValues.Add("DateCreated", oVenue.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }
    protected void fvVenueDetails_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        this.gvVenues.DataBind();
    }



    protected void gvVenues_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }


    protected void btnAddChildVenue_Click(object sender, EventArgs e)
    {
        if (this.gvVenues.Rows.Count > 0)
        {
            PlacementManager.VenueComponent oVenueC = new VenueComponent();
            PlacementManager.Venue oVenue = oVenueC.SelectOneWVenueName(this.txtAddChildVenue.Text);
            if (oVenue.VenueId > 0)
            {
                oVenue.ParentVenueId = Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString());
                oVenueC.Update(oVenue);
            }

            this.txtAddChildVenue.Text = string.Empty;
            this.gvChildVenues.DataBind();
        }
    }



    protected void btnAddCategories_Click(object sender, EventArgs e)
    {
        if (this.gvVenues.Rows.Count > 0)
        {
            PlacementManager.LinkVenueCategoryComponent oLvcC = new LinkVenueCategoryComponent();
            int venueId = Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString());


            foreach (ListItem li in this.lbAvailableCategories.Items)
            {
                if (li.Selected)
                {
                    PlacementManager.LinkVenueCategory oLvc = new LinkVenueCategory();
                    oLvc.CategoryId = Int32.Parse(li.Value);
                    oLvc.CreatedBy = User.Identity.Name;
                    oLvc.DateCreated = DateTime.Now;
                    oLvc.DateUpdated = DateTime.Now;
                    oLvc.IsActive = true;
                    oLvc.UpdatedBy = User.Identity.Name;
                    oLvc.VenueId = venueId;
                    oLvcC.Insert(oLvc);
                }
            }

            this.gvCategories.DataBind();
            this.lbAvailableCategories.DataBind();
        }
    }
    protected void lbAvailableCategories_DataBound(object sender, EventArgs e)
    {
        if (this.lbAvailableCategories.Items.Count > 0)
        {
            this.lbAvailableCategories.Visible = true;
            this.btnAddCategories.Visible = true;
            this.lblNoCategoriesAvailable.Visible = false;
        }
        else
        {
            this.lbAvailableCategories.Visible = false;
            this.btnAddCategories.Visible = false;
            this.lblNoCategoriesAvailable.Visible = true;
        }
    }
    protected void gvCategories_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteCategoryLink")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int categoryId = Int32.Parse(this.gvCategories.DataKeys[index].Value.ToString());
            int venueId = Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString());

            PlacementManager.LinkVenueCategoryComponent oLvcC = new PlacementManager.LinkVenueCategoryComponent();
            oLvcC.DeleteAllWVenueIdCategoryId(venueId, categoryId);


            this.gvCategories.DataBind();
            this.lbAvailableCategories.DataBind();
        }
    }
    protected void gvChildVenues_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "RemoveChildVenue")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int venueId = Int32.Parse(this.gvChildVenues.DataKeys[index].Value.ToString());

            PlacementManager.VenueComponent oVenueC = new VenueComponent();
            PlacementManager.Venue oVenue = oVenueC.SelectOne(venueId);
            oVenue.ParentVenueId = 0;
            oVenueC.Update(oVenue);

            this.gvChildVenues.DataBind();
        }
    }


    protected void gvVenueRequirements_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnSimpleSearch_Click(object sender, EventArgs e)
    {
        this.gvVenues.DataBind();
    }
    protected void gvVenues_DataBound(object sender, EventArgs e)
    {
        if (this.gvVenues.Rows.Count > 0)
        {
            this.fvVenueDetails.Visible = true;
            this.fvAddress.Visible = true;
            this.gvNotes.Visible = true;
            this.gvChildVenues.Visible = true;
            this.btnShowAddNewContact.Visible = true;

        }
        else
        {
            this.fvVenueDetails.Visible = false;
            this.fvAddress.Visible = false;
            this.gvNotes.Visible = false;
            this.gvChildVenues.Visible = false;
            this.btnShowAddNewContact.Visible = false;
        }
    }

    protected void ddNewContactCountry_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ddEditVenueCountry_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ddEditVenueState_DataBound(object sender, EventArgs e)
    {

    }
    protected void imgbtnSearchPossibleLocationsEdit_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void gvEditPossibleLocations_SelectedIndexChanged(object sender, EventArgs e)
    {

    }



    protected void btnDeleteVenue_Click(object sender, EventArgs e)
    {
        PlacementManager.VenueComponent oVenueC = new VenueComponent();
        PlacementManager.Venue oVenue = oVenueC.SelectOne(Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString()));

        oVenue.IsActive = false;
        oVenueC.Update(oVenue);

        this.gvVenues.DataBind();
    }

    protected void btnAddAddress_Click(object sender, EventArgs e)
    {



    }

    protected void imgbtnFindPostCode_Click(object sender, ImageClickEventArgs e)
    {
        PlacementManager.Geo_PostcodesComponent oPostCodeC = new Geo_PostcodesComponent();
        PlacementManager.Geo_Postcodes oPostCode = oPostCodeC.SelectOneWTownCode(this.txtNewCity.Text);
        this.txtNewPostCode.Text = oPostCode.Postcode.ToString();
    }
    protected void dlContacts_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int contactId = Int32.Parse(this.dlContacts.DataKeys[e.Item.ItemIndex].ToString());
        PlacementManager.LinkVenueContactComponent oLvcC = new LinkVenueContactComponent();
        oLvcC.DeleteAllWContactId(contactId);

        this.dlContacts.DataBind();
    }
    protected void dlContacts_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "MakeContactPrimary")
        {
            int contactId = Int32.Parse(this.dlContacts.DataKeys[e.Item.ItemIndex].ToString());
            PlacementManager.LinkVenueContactComponent oLvcC = new LinkVenueContactComponent();
            oLvcC.SetVenuePrimaryContact(contactId, Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString()));
            this.dlContacts.DataBind();
        }
    }
    protected void gvVenues_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }
    protected void gvVenues_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string venueName = DataBinder.Eval(e.Row.DataItem, "VenueName").ToString();

            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + venueName.Replace("'","\\'") + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
        }
    }
    protected void btnAddUnit_Click(object sender, EventArgs e)
    {
        if (this.gvVenues.Rows.Count > 0)
        {
            PlacementManager.LinkVenueUnitComponent oLvuC = new LinkVenueUnitComponent();
            PlacementManager.LinkVenueUnit oLvu = new LinkVenueUnit();
            oLvu.CampusId = Int32.Parse(this.ddNewLinkedCampus.SelectedValue);
            oLvu.CreatedBy = User.Identity.Name;
            oLvu.DateCreated = DateTime.Now;
            oLvu.DateUpdated = DateTime.Now;
            oLvu.IsActive = true;
            oLvu.UnitId = Int32.Parse(this.ddNewLinkedUnit.SelectedValue);
            oLvu.UpdatedBy = User.Identity.Name;
            oLvu.VenueId = Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString());

            oLvuC.Insert(oLvu);

            this.gvLinkedUnits.DataBind();
        }
    }


    protected void fvAddress_ItemDeleted(object sender, FormViewDeletedEventArgs e)
    {

    }
    protected void imgbtnEditFindPostCode_Click(object sender, ImageClickEventArgs e)
    {
        TextBox txtEditCity = (TextBox)this.fvAddress.FindControl("txtEditCity");
        if (txtEditCity != null)
        {
            PlacementManager.Geo_PostcodesComponent oPostCodeC = new Geo_PostcodesComponent();
            PlacementManager.Geo_Postcodes oPostCode = oPostCodeC.SelectOneWTownCode(txtEditCity.Text);

            TextBox txtEditPostCode = (TextBox)this.fvAddress.FindControl("txtEditPostCode");
            txtEditPostCode.Text = oPostCode.Postcode.ToString();
        }
    }
    protected void fvAddress_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        this.gvVenues.DataBind();
    }
    protected void fvAddress_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        PlacementManager.AddressComponent oAddressC = new AddressComponent();
        PlacementManager.Address oAddress = oAddressC.SelectOne(Int32.Parse(this.fvAddress.DataKey.Value.ToString()));

        //TextBox Address1TextBox = (TextBox)this.fvAddress.FindControl("Address1TextBox");
        //if (Address1TextBox.Text == string.Empty)
        //{
        //    e.Keys["Address1"] = "";
        //}

        //TextBox Address2TextBox = (TextBox)this.fvAddress.FindControl("Address2TextBox");
        //if (Address2TextBox.Text == string.Empty)
        //{
        //    e.Keys["Address2"] = "";
        //}


        e.NewValues.Add("AddressId", oAddress.AddressId);
        e.NewValues.Add("IsActive", true);
        e.NewValues.Add("CreatedBy", oAddress.CreatedBy);
        e.NewValues.Add("DateCreated", oAddress.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);

        e.NewValues.Add("Latitude", 0);
        e.NewValues.Add("Longitude", 0);


    }

    protected void btnSaveVenue_Click(object sender, EventArgs e)
    {
        if (this.txtNewVenueName.Text.Length > 0)
        {
            try
            {
                //Save the new venue's name
                PlacementManager.VenueComponent oVenueC = new VenueComponent();
                PlacementManager.Venue oNewVenue = new PlacementManager.Venue();
                oNewVenue.CreatedBy = User.Identity.Name;
                oNewVenue.DateCreated = DateTime.Now;
                oNewVenue.DateUpdated = DateTime.Now;
                oNewVenue.IsActive = true;
                oNewVenue.ParentVenueId = 0;
                oNewVenue.TelephoneNumber = this.txtNewVenueTelephone.Text.Trim();
                oNewVenue.Fax = this.txtNewVenueFax.Text.Trim();
                oNewVenue.GenericEmail = this.txtNewVenueGenericEmail.Text.Trim();
                oNewVenue.UpdatedBy = User.Identity.Name;
                oNewVenue.VenueName = this.txtNewVenueName.Text.Trim();
                int venueId = oVenueC.Insert(oNewVenue);

                this.txtSearchName.Text = this.txtNewVenueName.Text.Trim();
                this.txtNewVenueName.Text = string.Empty;
                this.txtNewVenueTelephone.Text = string.Empty;
                this.txtNewVenueFax.Text = string.Empty;
                this.txtNewVenueGenericEmail.Text = string.Empty;



                //Create some objects that we will use later to save creating them separately
                //A Google Map object so we can try and resolve the coordinates of the address
                PlacementManager.GoogleMap oGM = new PlacementManager.GoogleMap();

                //Get an address string to check for geo-coordinates
                string address = this.txtNewAddress1.Text + ",";
                if (this.txtNewAddress2.Text.Length > 0)
                {
                    address += this.txtNewAddress2 + ",";
                }
                address += this.txtNewCity.Text + ", " + this.ddNewState.SelectedItem.Text + ", " + this.ddNewCountry.SelectedItem.Text;

                //Let's try and get the coordinates
                float[] coordinates = oGM.GetLongitudeLatitude(address);

                PlacementManager.AddressComponent oAddressC = new AddressComponent();
                PlacementManager.Address oAddress = new PlacementManager.Address();
                oAddress.Address1 = this.txtNewAddress1.Text;
                oAddress.Address2 = this.txtNewAddress2.Text;
                oAddress.Street = this.txtNewStreet.Text;
                oAddress.City = this.txtNewCity.Text;
                oAddress.CountryId = Int32.Parse(this.ddNewCountry.SelectedValue);
                oAddress.CreatedBy = User.Identity.Name;
                oAddress.DateCreated = DateTime.Now;
                oAddress.DateUpdated = DateTime.Now;
                oAddress.IsActive = true;
                oAddress.IsPostal = true;
                oAddress.Latitude = coordinates[1];
                oAddress.Longitude = coordinates[0];
                oAddress.PostCode = txtNewPostCode.Text;
                oAddress.StateId = Int32.Parse(this.ddNewState.SelectedValue);
                oAddress.UpdatedBy = User.Identity.Name;

                int addressId = oAddressC.Insert(oAddress);

                PlacementManager.LinkVenueAddressComponent oLvac = new LinkVenueAddressComponent();
                PlacementManager.LinkVenueAddress oLva = new LinkVenueAddress();

                oLva.AddressId = addressId;
                oLva.CreatedBy = User.Identity.Name;
                oLva.DateCreated = DateTime.Now;
                oLva.DateUpdated = DateTime.Now;
                oLva.IsActive = true;
                oLva.IsPrimary = true;
                oLva.UpdatedBy = User.Identity.Name;
                oLva.VenueId = venueId;

                oLvac.Insert(oLva);

                this.txtNewAddress1.Text = string.Empty;
                this.txtNewAddress2.Text = string.Empty;
                this.txtNewStreet.Text = string.Empty;
                this.txtNewCity.Text = string.Empty;
                this.txtNewPostCode.Text = string.Empty;

                this.gvVenues.DataBind();

            }
            catch (Exception exc)
            {
                this.lblAddNewVenueMessage.Text = "There was a problem adding the venue.";
            }
            finally
            {

            }
        }
    }
    protected void btnAddNewContact_Click(object sender, EventArgs e)
    {
        if (this.txtNewContactLastName.Text.Length > 0 && this.gvVenues.Rows.Count > 0)
        {
            int existingContactId = 0;
            PlacementManager.ContactComponent oContactC = new PlacementManager.ContactComponent();

            if (this.txtNewContactEmail.Text.Length > 0)//Need to check that the same email doesn't exits already
            {
                PlacementManager.Contact oExistingContact = oContactC.SelectOneWEmailAddress(this.txtNewContactEmail.Text.Trim());
                existingContactId = oExistingContact.ContactId;
            }

            if (existingContactId == 0)
            {
                PlacementManager.Contact oContact = new PlacementManager.Contact();
                int venueId = Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString());

                //Create a new contact
                oContact.CreatedBy = User.Identity.Name;
                oContact.Title = this.txtNewContactTitle.Text;
                oContact.DateCreated = DateTime.Now;
                oContact.DateUpdated = DateTime.Now;
                oContact.DivisionId = Int32.Parse(Session["DivisionId"].ToString());
                oContact.EmailAddress = this.txtNewContactEmail.Text;
                oContact.FirstName = this.txtNewContactFirstName.Text;
                oContact.EducatorTypeId = 1;
                oContact.IsActive = true;
                oContact.JobTitle = this.txtNewContactJobTitle.Text;
                oContact.LastName = this.txtNewContactLastName.Text;
                oContact.UpdatedBy = User.Identity.Name;
                int contactId = oContactC.Insert(oContact);

                //Now create a link between the contact and this venue
                PlacementManager.LinkVenueContactComponent oLvcC = new LinkVenueContactComponent();
                PlacementManager.LinkVenueContact oLvc = new LinkVenueContact();
                oLvc.CreatedBy = User.Identity.Name;
                oLvc.ContactId = contactId;
                oLvc.DateCreated = DateTime.Now;
                oLvc.DateUpdated = DateTime.Now;
                oLvc.IsActive = true;
                oLvc.IsPrimary = this.cbNewContactIsPrimary.Checked;
                oLvc.UpdatedBy = User.Identity.Name;
                oLvc.VenueId = venueId;
                oLvcC.Insert(oLvc);
                if (this.cbNewContactIsPrimary.Checked)
                {
                    oLvcC.SetVenuePrimaryContact(contactId, venueId);
                }

                this.dlContacts.DataBind();

                this.lblAddNewContactMessage.Text = "The contact was added succesfully.";
                this.txtNewContactTitle.Text = string.Empty;
                this.txtNewContactFirstName.Text = string.Empty;
                this.txtNewContactLastName.Text = string.Empty;
                this.txtNewContactJobTitle.Text = string.Empty;
                this.txtNewContactEmail.Text = string.Empty;
                this.cbNewContactIsPrimary.Checked = false;
            }
            else
            {
                this.lblAddNewContactMessage.Text = "The email address " + this.txtNewContactEmail.Text + " already exists in the database. Please re-enter the correct email address or click <a href=\"Contacts.aspx?email=" + this.txtNewContactEmail.Text + "\">here</a> to view the contact with this email address. You can then associate the contact with this venue from the contact detail screen.";
                this.txtNewContactEmail.Text = string.Empty;
            }

        }
    }
    protected void imgbtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.gvVenues.DataSourceID = "odsVenues_GV";
        this.gvVenues.PageIndex = 0;
        this.gvVenues.SelectedIndex = 0;
        this.gvVenues.DataBind();
    }
    protected void btnSaveNote_Click(object sender, EventArgs e)
    {
        if (this.gvVenues.Rows.Count > 0)
        {
            PlacementManager.NoteComponent oNoteC = new PlacementManager.NoteComponent();
            PlacementManager.Note oNote = new PlacementManager.Note();
            oNote.CreatedBy = User.Identity.Name;
            oNote.DateCreated = DateTime.Now;
            oNote.DateUpdated = DateTime.Now;
            oNote.EntityId = Int32.Parse(this.gvVenues.SelectedValue.ToString());
            oNote.IsActive = true;
            oNote.NoteTypeId = 2;
            oNote.UpdatedBy = User.Identity.Name;
            oNote.NoteText = this.txtAddNewNote.Text;
            oNoteC.Insert(oNote);

            this.gvNotes.DataBind();
            this.txtAddNewNote.Text = string.Empty;
        }
    }
    protected void gvVenues_PageIndexChanged(object sender, EventArgs e)
    {
        this.gvVenues.SelectedIndex = 0;
    }
    protected void gvNotes_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PlacementManager.NoteComponent oNoteC = new NoteComponent();
        PlacementManager.Note oNote = oNoteC.SelectOne(Int32.Parse(this.gvNotes.DataKeys[this.gvNotes.EditIndex].Value.ToString()));

        e.NewValues.Add("CreatedBy", oNote.CreatedBy);
        e.NewValues.Add("DateCreated", oNote.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("IsActive", oNote.IsActive);
        e.NewValues.Add("EntityId", oNote.EntityId);
        e.NewValues.Add("NoteId", oNote.NoteId);
        e.NewValues.Add("NoteTypeId", oNote.NoteTypeId);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }

    protected void gvNotes_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteNote")
        {
            PlacementManager.NoteComponent oNoteC = new NoteComponent();
            PlacementManager.Note oNote = oNoteC.SelectOne(Int32.Parse(e.CommandArgument.ToString()));
            oNote.IsActive = false;
            oNoteC.Update(oNote);
            this.gvNotes.DataBind();

        }
    }
    protected void dlContacts_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        int contactid = Int32.Parse(DataBinder.Eval(e.Item.DataItem, "ContactId").ToString());
        bool isPrimaryContact = this.GetIsPrimaryContact(contactid);
        if (isPrimaryContact)
        {
            e.Item.CssClass = "PrimaryContactItem";
            ImageButton imgbtnDeleteContact = (ImageButton)e.Item.FindControl("imgbtnDeleteContact");
            imgbtnDeleteContact.Visible = false;
        }

    }
    protected void gvLinkedUnits_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteUnitLink")
        {
            int venueUnitId = Int32.Parse(e.CommandArgument.ToString());

            PlacementManager.LinkVenueUnitComponent oLvuC = new PlacementManager.LinkVenueUnitComponent();
            oLvuC.Delete(venueUnitId);

            this.gvLinkedUnits.DataBind();
        }
    }
}
